﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AtletIS.DomainModel.DTOs;
using AtletIS.DomainModel.Entities.Main;
using AtletIS.Service.Constants;
using AtletIS.Service.Helpers;
using AtletIS.Service.Base;
using AtletIS.Repository.UnitOfWork;

namespace AtletIS.Service.Services
{
    public class StartListService : ServiceBase
    {
        private const string SMALL_FLAG_PATH = "../Resources/Flags/16px/{0}.png";
        private const string BIG_FLAG_PATH = "../Resources/Flags/24px/{0}.png";

        #region Constructor

        public StartListService()
        {
            this._unitOfWork = new UnitOfWork();
        }

        public StartListService(IUnitOfWork unitOfWork)
        {
            this._unitOfWork = unitOfWork;
        }

        #endregion

        #region Start list HTML string

        /// <summary>
        ///  Vráti HTML štartovnú listinu
        /// </summary>
        public string GetStartListAsHtml(int disciplineListId)
        {
            DisciplineList disciplineList = new DisciplineListService().GetById(disciplineListId);

            if (disciplineList != null)
            {
                return GetStartListAsHtml(disciplineList);
            }

            return string.Empty;
        }

        /// <summary>
        ///  Vráti HTML štartovnú listinu
        /// </summary>
        public string GetStartListAsHtml(DisciplineList disciplineList)
        {
            string result = string.Empty;

            result += "<h2>" + disciplineList.Name + "</h2>" + Environment.NewLine;
            result += "<table>" + Environment.NewLine;
            result += "<tr>" + Environment.NewLine;
            result += "  <th>Št.č.</th>" + Environment.NewLine;
            result += "  <th>Vlajka</th>" + Environment.NewLine;
            if (disciplineList.DisciplineCode == MainConstants.SPRINT_CODE)
            {
                result += "  <th>Dráha</th>" + Environment.NewLine;
            }
            result += "  <th>Meno</th>" + Environment.NewLine;
            result += "  <th>Nar.</th>" + Environment.NewLine;
            result += "  <th>Krajina</th>" + Environment.NewLine;
            result += "  <th>PB</th>" + Environment.NewLine;
            result += "  <th>SB</th>" + Environment.NewLine;
            result += "</tr>" + Environment.NewLine;

            int totalCount;
            foreach (StartListDto startListDto in this.GetStartListCollection(disciplineList, out totalCount))
            {
                result += "<tr>" + Environment.NewLine;
                result += "  <td>(" + startListDto.StartNumber + ")</td>" + Environment.NewLine;
                result += "  <td><img src=\"Ext/Flags/16px/" + startListDto.CountryId + ".png\"/></td>" + Environment.NewLine;
                if (disciplineList.DisciplineCode == MainConstants.SPRINT_CODE)
                {
                    result += "  <td>(" + startListDto.Lane + ")</td>" + Environment.NewLine;
                }
                result += "  <td>" + startListDto.FullName + "</td>" + Environment.NewLine;
                result += "  <td>" + startListDto.BirthDate + "</td>" + Environment.NewLine;
                result += "  <td>" + startListDto.Country + "</td>" + Environment.NewLine;
                result += "  <td>" + startListDto.PB + "</td>" + Environment.NewLine;
                result += "  <td>" + startListDto.SB + "</td>" + Environment.NewLine;
                result += "</tr>" + Environment.NewLine;
            }

            result += "</table>";
            return result;
        }

        #endregion

        #region Start list collection

        /// <summary>
        ///  Vráti kolekciu dát pre štartovnú listinu
        /// </summary>
        public List<StartListDto> GetStartListCollection(DisciplineList disciplineList, out int totalCount, int? page = null, int? pageSize = null)
        {
            List<StartListDto> startList = new List<StartListDto>();

            List<Performance> performances = new PerformancesService()
                .GetByDisciplineListId(disciplineList.ID, Enums.PerformancesSortEnum.StartList, out totalCount, false, false, page, pageSize);

            if (performances != null && performances.Count > 0)
            {
                foreach (Performance performance in performances)
                {
                    StartListDto startListDto = this.ConvertPerformanceToStartListDto(performance, disciplineList);
                    startList.Add(startListDto);
                }
            }

            return startList;
        }

        #endregion

        #region Performance to DTO

        /// <summary>
        ///  Konvertuje Performance na StartListDto
        /// </summary>
        private StartListDto ConvertPerformanceToStartListDto(Performance performance, DisciplineList disciplineList)
        {
            StartListDto startListDto = new StartListDto();

            // Start number
            if (performance.StartNumber.HasValue && performance.StartNumber > 0)
            {
                startListDto.StartNumber = performance.StartNumber.Value.ToString();
            }
            else
            {
                startListDto.StartNumber = string.Empty;
            }

            // Lane
            if (performance.Lane.HasValue && performance.Lane > 0)
            {
                startListDto.Lane = string.Format("({0})", performance.Lane.Value.ToString());
            }
            else
            {
                startListDto.Lane = string.Empty;
            }

            // FullName
            startListDto.FullName = string.Format("{0} {1}", performance.Surname.ToUpper(), performance.FirstName);

            // BirthDate
            if (performance.Birthdate.HasValue)
            {
                startListDto.BirthDate = PrintHelper.PrintDate(performance.Birthdate.Value);
                startListDto.BirthYear = performance.Birthdate.Value.Year.ToString();
            }
            else
            {
                startListDto.BirthDate = string.Empty;
                startListDto.BirthYear = string.Empty;
            }

            // Country
            startListDto.CountryId = performance.CountryId;
            if (performance.Country != null)
            {
                startListDto.Country = performance.Country.Name;
            }

            // Club
            startListDto.Club = performance.Club;

            // SB, PB
            startListDto.SB = PrintHelper.PrintResult(performance.SB.Value.ToString(), disciplineList.DisciplineCode.Value);
            startListDto.PB = PrintHelper.PrintResult(performance.PB.Value.ToString(), disciplineList.DisciplineCode.Value);

            // Flag path
            startListDto.SmallFlagPath = string.Format(SMALL_FLAG_PATH, startListDto.CountryId);
            startListDto.BigFlagPath = string.Format(BIG_FLAG_PATH, startListDto.CountryId);
            
            // Info
            startListDto.Info = string.Format("{0} \u2022 {1}", startListDto.BirthYear, 
                disciplineList.Competition.ClubsCountries ? startListDto.Club : startListDto.Country);

            return startListDto;
        }

        #endregion

    }
}
